import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';

import '../../../../bean/CustomerList.dart';
import '../../../../config/RouteConfig.dart';
import '../../../../config/StyleConfig.dart';
import '../../../../res/HDColors.dart';
import '../../../../util/HDNavigator.dart';
import '../../../../view/common_widget.dart';
import '../../../../view/empty_data_widget.dart';
import 'logic.dart';

class StationDetailPage extends StatelessWidget {
  StationDetailPage({Key? key}) : super(key: key);

  final logic = Get.put(StationDetailLogic());
  final state = Get.find<StationDetailLogic>().state;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(onWillPop: () async {
      logic.closePage();
      return false;
    }, child: GetBuilder<StationDetailLogic>(builder: (logic) {
      return Scaffold(
        appBar: AppBar(
            title: Text("${state.stationBean.stationName}"),
            elevation: 0,
            backgroundColor: HDColors.green,
            leading: CommonWidget.getAppBarLeading()),
        body: SingleChildScrollView(
          child: Column(
            children: [
              Container(
                color: HDColors.green,
                padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
                child: Column(
                  children: [
                    Row(
                      children: [
                        Expanded(
                            child: Text(
                                "类型：${state.stationBean.stationType == 1 ? "光伏" : "储能"}" ??
                                    "",
                                style: const TextStyle(
                                    fontSize: StyleConfig.fontContentSize16,
                                    color: HDColors.white))),
                        CommonWidget.buildIconWidget(
                            showIcon: false, nodestatue: "")
                      ],
                    ),
                    SizedBox(height: 5),
                    Row(mainAxisAlignment: MainAxisAlignment.start, children: [
                      Icon(color: HDColors.blue, Icons.location_on_outlined),
                      SizedBox(width: 15),
                      Text(state.stationBean.address ?? "",
                          style: TextStyle(
                              fontSize: StyleConfig.fontContentSize16,
                              color: HDColors.white)),
                    ]),
                    SizedBox(height: 5),
                    Row(
                      children: [
                        Text("装机容量 ${state.stationBean.installCapacity}" ?? "",
                            style: TextStyle(
                                fontSize: StyleConfig.fontContentSize16,
                                color: HDColors.white))
                      ],
                    )
                  ],
                ),
              ),
              SizedBox(height: 15,),
              Card(
                margin: EdgeInsets.fromLTRB(20, 0, 20, 0),
                color: HDColors.white,
                shadowColor: HDColors.grayHint,
                elevation: 1,
                shape: RoundedRectangleBorder(
                    side: BorderSide(
                        width: 0.3, color: HDColors.divider),
                    borderRadius:
                    BorderRadius.all(new Radius.circular(5))),
                child: Container(
                    width: double.infinity,
                    height: 170,
                    child: Stack(children: [
                      Align(
                        alignment: Alignment.topCenter,
                        child: BrnDoughnutChart(
                          padding: EdgeInsets.all(15),
                          ringWidth: 10,
                          width: 90,
                          height: 90,
                          data: [
                            BrnDoughnutDataItem(
                                title: '示例',
                                value: state.todayPower,
                                color: Colors.blue),
                            BrnDoughnutDataItem(
                                title: '示例',
                                value: state.monthPower,
                                color: HDColors.grayHint)
                          ],
                          showTitleWhenSelected: true,
                          selectCallback: (BrnDoughnutDataItem?
                          selectedItem) {},
                        ),
                      ),
                      Positioned(
                        left: 50,
                        bottom: 20,
                        child: Column(
                            crossAxisAlignment:
                            CrossAxisAlignment.center,
                            children: [
                              Text(state.todayPower.toString(),
                                  style: TextStyle(
                                    fontSize: StyleConfig
                                        .fontContentSize14,
                                  )),
                              SizedBox(height: 5),
                              Text("今日发电量(度)",
                                  style: TextStyle(
                                      fontSize: StyleConfig
                                          .fontContentSize12,
                                      color:
                                      HDColors.formTitleColor)),
                            ]),
                      ),
                      Positioned(
                        right: 50,
                        bottom: 20,
                        child: Column(
                            crossAxisAlignment:
                            CrossAxisAlignment.center,
                            children: [
                              Text(state.monthPower.toString(),
                                  style: TextStyle(
                                    fontSize: StyleConfig
                                        .fontContentSize14,
                                  )),
                              SizedBox(height: 5),
                              Text("本月发电量(度)",
                                  style: TextStyle(
                                      fontSize: StyleConfig
                                          .fontContentSize12,
                                      color:
                                      HDColors.formTitleColor)),
                            ]),
                      ),
                    ])),
              ),
              Container(height: 15),
              Card(
                margin: EdgeInsets.fromLTRB(20, 0, 20, 20),
                color: HDColors.white,
                shadowColor: HDColors.grayHint,
                elevation: 1,
                shape: RoundedRectangleBorder(
                    side: BorderSide(
                        width: 0.3, color: HDColors.divider),
                    borderRadius:
                    BorderRadius.all(new Radius.circular(5))),
                child: Container(
                    width: double.infinity,
                    height: 490,
                    child: Stack(children: [
                      Positioned(
                        left: 20,
                        top: 10,
                        child: Text("发电量统计",
                            style: TextStyle(
                              fontSize:
                              StyleConfig.fontContentSize14,
                            )),
                      ),
                      Positioned(
                        left: 20,
                        top: 40,
                        child: BrnSelectTag(
                            tags: ["月", "年"],
                            spacing: 20,
                            tagWidth: 30,
                            initTagState: [true],
                            onSelect: (selectedIndexes) {
                              state.isMonth = selectedIndexes[0];
                              logic.requestChart(false);
                            }),
                      ),
                      Positioned(
                        left: 20,
                        top: 80,
                        child: IconButton(
                          onPressed: () async {
                            logic.getYearData("back");
                          },
                          icon: const Icon(
                            Icons.arrow_back_ios,
                            size: 20.0,
                          ),
                        ),
                      ),
                      Positioned(
                          top: 80,
                          left: 0,
                          right: 0,
                          child: Container(
                              height: 50,
                              alignment: Alignment.center,
                              child: Column(
                                  mainAxisAlignment:
                                  MainAxisAlignment.center,
                                  children: [
                                    Visibility(
                                        visible: state.isMonth == 0,
                                        child: Text(
                                            state.month.toString(),
                                            style: TextStyle(
                                              fontSize: StyleConfig
                                                  .fontContentSize14,
                                            ))),
                                    Text(state.year.toString(),
                                        style: TextStyle(
                                          fontSize: StyleConfig
                                              .fontContentSize14,
                                        ))
                                  ]))),
                      Positioned(
                        right: 20,
                        top: 80,
                        child: IconButton(
                          onPressed: () async {
                            logic.getYearData("add");
                          },
                          icon: const Icon(
                            Icons.arrow_forward_ios,
                            size: 20.0,
                          ),
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.fromLTRB(0, 130, 0, 0),
                        child:  _brokenLine2(context),
                      )
                    ])),
              )
            ],
          ),
        ),
      );
    }));
  }

  Widget _brokenLine2(context) {
    List<AxisItem> axisItemListData = [];
    List<BrnProgressBarItem> barListData = [];
    List<BrnProgressBarItem> pubListData = [];
    for (int i = 0; i < state.chartList.length; i++) {
      axisItemListData.add(AxisItem(showText: (i+1).toString()));
    }
    for (int i = 0; i < state.chartList.length; i++) {
      pubListData.add(BrnProgressBarItem(
          text: state.chartList[i].attribValue.toString(), value:state.chartList[i].attribValue??0));
    }
    return Container(
      child: Column(
        children: <Widget>[
          Row(
            children: [
              SizedBox(width: 20),
              Text("发电量",
                  style: TextStyle(
                      fontSize: StyleConfig.fontContentSize12,
                      color: HDColors.formTitleColor)),
              Icon(
                Icons.rectangle,
                color: HDColors.blue,
                size: 17,
              ),
            ],
          ),
          BrnProgressBarChart(
            barChartStyle: BarChartStyle.vertical,
            xAxis: ChartAxis(axisItemList:axisItemListData),
            barBundleList: [
              BrnProgressBarBundle(barList:pubListData
                  , colors: [
                    Color(0xff1545FD),
                    Color(0xff0984F9)
                  ]),
            ],
            yAxis: ChartAxis(axisItemList: [
              AxisItem(showText: (state.barMaxValue / 6).toInt().toString()),
              AxisItem(showText: ((state.barMaxValue / 6)*2).toInt().toString()),
              AxisItem(showText: ((state.barMaxValue / 6)*3).toInt().toString()),
              AxisItem(showText: ((state.barMaxValue / 6)*4).toInt().toString()),
              AxisItem(showText: ((state.barMaxValue / 6)*5).toInt().toString()),
              AxisItem(showText: (state.barMaxValue).toString())
            ]),
            singleBarWidth: 12,
            barGroupSpace: 5,
            barMaxValue: state.barMaxValue.toDouble(),
            onBarItemClickInterceptor: (barBundleIndex, barBundle, barGroupIndex, barItem) {
              return true;
            },
          ),
        ],
        crossAxisAlignment: CrossAxisAlignment.start,
      ),
    );
  }
}
